#include "stdafx.h"
#include "PyRoad.h"
#include "PythonObject.h"
#include "LoadUICfg.h"
#ifdef USE_SCRIPT_PYTHON
using namespace boost::python;

PyRoad::PyRoad()
{
}


PyRoad::~PyRoad()
{
}

bp::tuple PyRoad::GetMilePnt(const ZMReal& fMile)
{
	ZMReal x, y, z, fAngle;
	int iRet = GetMilePntCore(x, y, z, fAngle, fMile);
	return bp::make_tuple(iRet, x, y, z, fAngle);
}

bp::tuple PyRoad::GetMileDir(const ZMReal& fMile)
{
	ZMReal x, y, z;
	int iRet = GetMileDirCore(x, y, z, fMile);
	return bp::make_tuple(iRet, x, y, z);
}

bp::tuple PyRoad::GetMileVeDir(const ZMReal& fMile)
{
	ZMReal x, y, z;
	int iRet = GetMileVeDirCore(x, y, z, fMile);
	return bp::make_tuple(iRet, x, y, z);
}

bp::tuple PyRoad::GetRoadData(const ZMReal& fMile, const ZMReal& fDistance, const bool& bLeft)
{
	ZMReal x, y, z;
	int iRet = GetRoadDataCore(x, y, z, fMile, fDistance, bLeft);
	return bp::make_tuple(iRet, x, y, z);
}

PRoadDesign PyRoad::GetCurrentRoad()
{
	ZMString strMeshName, strScriptName;
	PythonObject::GetCurrentMeshName(strMeshName, strScriptName);
	if (!(strMeshName.empty() || strScriptName.empty()))
	{
		ZMVector<UITemplate> vTemplate = CLoadUICfg::GetInstance()->GetObjUITemplate(ToQString(strMeshName));
		for (UITemplate uiTemplate : vTemplate)
		{
			if (uiTemplate.GetScriptFileName() == ToQString(strScriptName))
			{
				ZMString strRoadName = ToString(uiTemplate.GetRoadName());
				PRoadGroup pRoadGroup = GetMainWnd()->GetRoadGroup();
				return pRoadGroup->FindRoad(strRoadName);
			}
		}
	}
	return PRoadDesign();
}

int PyRoad::GetMilePntCore(ZMReal& x, ZMReal& y, ZMReal&z, ZMReal& fAngle, const ZMReal& fMile)
{
	PRoadDesign pRoad = GetCurrentRoad();
	SharedPointer<RoadMileLine> pRoadMile = pRoad->GetCrossData()->GetMileData();
	ZMPoint3D pnt;
	int iRet = pRoadMile->GetMileData(fMile, pnt, fAngle);
	x = pnt[0];
	y = pnt[1];
	z = pnt[2];
	return iRet;
}

int PyRoad::GetMileDirCore(ZMReal& x, ZMReal& y, ZMReal&z, const ZMReal& fMile)
{
	PRoadDesign pRoad = GetCurrentRoad();
	SharedPointer<RoadMileLine> pRoadMile = pRoad->GetCrossData()->GetMileData();
	ZMPoint3D dir;
	dir = pRoadMile->GetMileDir(fMile);
	if (dir.squared_length() > 0)
	{
		x = dir[0];
		y = dir[1];
		z = dir[2];
		return 0;
	}

	return 1;
}

int PyRoad::GetMileVeDirCore(ZMReal& x, ZMReal& y, ZMReal&z, const ZMReal& fMile)
{
	PRoadDesign pRoad = GetCurrentRoad();
	SharedPointer<RoadMileLine> pRoadMile = pRoad->GetCrossData()->GetMileData();
	ZMPoint3D dir;
	dir = pRoadMile->GetVerticalDir(fMile);
	if (dir.squared_length() > 0)
	{
		x = dir[0];
		y = dir[1];
		z = dir[2];
		return 0;
	}
	return 1;
}

int PyRoad::GetRoadDataCore(ZMReal& x, ZMReal& y, ZMReal&z, const ZMReal& fMile, const ZMReal& fDistance, const bool& bLeft)
{
	PRoadDesign pRoad = GetCurrentRoad();
	ZMPoint3D pnt;
	RoadCrossSectionalDesign::emObjectType objType;
	pRoad->GetCrossData()->GetCrossRoadValue(pnt, objType, *(pRoad->GetTerrian()), fMile, fDistance, bLeft);

	x = pnt[0];
	y = pnt[1];
	z = pnt[2];
	return (int)objType;
}

BOOST_PYTHON_MODULE(py_road)
{
	bp::class_<PyRoad>("road", no_init)
		.def("GetMilePnt", &PyRoad::GetMilePnt, (bp::arg("fMile")))
		.def("GetMileDir", &PyRoad::GetMileDir, (bp::arg("fMile")))
		.def("GetMileVeDir", &PyRoad::GetMileVeDir, (bp::arg("fMile")))
		.def("GetRoadData", &PyRoad::GetRoadData, (bp::arg("fMile"), bp::arg("fDistance"), bp::arg("bLeft") = true));
}
#endif